package com.hgsoft.pubsv.component.xxlrpc.config;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import com.hgsoft.pubsv.component.xxlrpc.properties.XxlRpcProperties;
import com.xxl.rpc.core.registry.impl.XxlRpcAdminRegister;
import com.xxl.rpc.core.remoting.net.impl.netty.server.NettyServer;
import com.xxl.rpc.core.remoting.provider.impl.XxlRpcSpringProviderFactory;
import com.xxl.rpc.core.serialize.impl.HessianSerializer;

import lombok.extern.slf4j.Slf4j;

/**
 * rpc 服务端bean加载
 * @author saint
 */
@Slf4j
public class XxlRpcProviderConfig {
	
	@Autowired
	private XxlRpcProperties xxlRpcProperties;

    @SuppressWarnings("serial")
	@Bean
    public XxlRpcSpringProviderFactory xxlRpcSpringProviderFactory() {
        XxlRpcSpringProviderFactory providerFactory = new XxlRpcSpringProviderFactory();
        providerFactory.setServer(NettyServer.class);
        providerFactory.setSerializer(HessianSerializer.class);
        providerFactory.setCorePoolSize(xxlRpcProperties.getCorePoolSize());
        providerFactory.setMaxPoolSize(xxlRpcProperties.getMaxPoolSize());
        providerFactory.setIp(xxlRpcProperties.getIp());
        providerFactory.setPort(xxlRpcProperties.getPort());
        providerFactory.setAccessToken(xxlRpcProperties.getAccessToken());
        providerFactory.setServiceRegistry(XxlRpcAdminRegister.class);
        providerFactory.setServiceRegistryParam(new HashMap<String, String>() {{
            put(XxlRpcAdminRegister.ADMIN_ADDRESS, xxlRpcProperties.getAdminAddress());
            put(XxlRpcAdminRegister.ENV, xxlRpcProperties.getEnv());
            put(XxlRpcAdminRegister.BIZ, xxlRpcProperties.getBiz());
        }});
        log.info(">>>>>>>>>>> xxl-rpc provider config init finish.");
        return providerFactory;
    }

}
